package leetcode.problems;

import org.junit.Test;

import java.util.Arrays;

/**
 * Created by gmwang on 2018/7/25
 * 转置矩阵
 */
public class _0802TransposeMatrix {
    /**
     * Given a matrix A, return the transpose of A.
     * 给定矩阵A，返回A的转置。
     *
     * The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix.
     * 矩阵的转置是在它的主对角线上翻转的矩阵，转换矩阵的行和列索引。
     *
     * Example 1:
     *
     * Input: [[1,2,3],[4,5,6],[7,8,9]]
     * Output: [[1,4,7],[2,5,8],[3,6,9]]
     * 1 2 3      1 4 7
     * 4 5 6  =》 2 5 8
     * 7 8 9      3 6 9
     * Example 2:
     *
     * Input: [[1,2,3],[4,5,6]]
     * Output: [[1,4],[2,5],[3,6]]
     * 1 2 3 =>  1 4
     * 4 5 6     2 5
     *           3 6
     * Note:
     *
     * 1 <= A.length <= 1000
     * 1 <= A[0].length <= 1000
     *
    /**
     *
     * @param A,B
     * @return
     */
    public int[][] transpose(int[][] A) {
        int len1 = A[0].length>=A.length?A[0].length:A.length;
        int len2 = A[0].length>=A.length?A.length:A[0].length;
        int [][] res;
        int len = 0;
        if(A[0].length>=A.length){
            res = new int[len1][len2];
            len = len2;
        }else{
            res = new int[len2][len1];
            len = len1;
        }
        for(int i = 0;i < len;i ++){
            for(int y = 0;y<A[i].length;y++){
                System.out.println(A[i][y]);
                res[y][i] = A[i][y];
            }
        }
        return res;
    }
    @Test
    public void test() {
//        int[][] ints = {{1,2,3},{4,5,6},{7,8,9}};
//        int[][] ints = {{1,2,3},{4,5,6}};
//        int[][] ints = {{5},{8}};
        int[][] ints = {{1,1,1}};
        int[][] res = transpose(ints);
        for(int i =0;i<res.length;i++)
        System.out.println(Arrays.toString(res[i]));
    }
}
